/*
 * @Author: 肖芬芳 1911683843@qq.com
 * @Date: 2022-12-08 10:51:51
 * @LastEditors: 肖芬芳 1911683843@qq.com
 * @LastEditTime: 2022-12-09 14:34:32
 * @FilePath: \blog\routes\admin\goods\class\addSecondClass.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const {
    SecondClass
} = require('../../../../model/goodSecondClass');
const {
    FirstClass
} = require('../../../../model/goodFirstClass');
const { ObjectId } = require('mongodb');

module.exports = async (req, res) => {
    // console.log(req.fields)
    let {firstClassName}=await FirstClass.findOne({_id:ObjectId(req.fields.firstClassId)})
    SecondClass.create({
        secondClassName: req.fields.secondClassName,
        firstClassName:firstClassName,
        firstClassId:req.fields.firstClassId,
        goodsNum:0,
        status: 1,
        createTime:new Date().getTime(),
    })
        .then(result => console.log(result))
        .catch(err => console.log(err));

    // 二级分类数量增加1
    await FirstClass.updateOne({_id:ObjectId(req.fields.firstClassId)},{$inc:{secondClassNum:1}})
    res.send({ 'code': 0, "msg": "新增成功" });
}